/*
 * DiscoverySearchAccurateFragment.java
 * business
 *
 * Created by chentao on 2017/4/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.discovery.searchaccurate;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.data.recyclertype.Article;
import com.yidiandao.business.ui.main.MainActivity;
import com.yidiandao.business.ui.main.discovery.DiscoveryFragment;
import com.yidiandao.business.ui.widget.recyclerview.LoadMoreRecyclerContainer;
import com.yidiandao.business.ui.widget.recyclerview.MultiTypeLoadMoreAdapter;
import com.yidiandao.business.ui.widget.recyclerview.provider.DiscoverySearchArticleProvider;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;

/**
 * 发现-精确搜索-View
 */
public class DiscoverySearchAccurateFragment extends SuperBaseFragment implements DiscoverySearchAccurateContract.View {

    private DiscoverySearchAccurateContract.Presenter mPresenter;

    private ImageButton mIbtnBack;

    private TextView mTvCancel;

    private TextView mEtSearch;

    private RecyclerView mRecyclerView;

    private LoadMoreRecyclerContainer mLoadMoreContainer;

    private MultiTypeLoadMoreAdapter mAdapter;

    public static DiscoverySearchAccurateFragment getInstance() {
        DiscoverySearchAccurateFragment fragment = new DiscoverySearchAccurateFragment();
        return fragment;
    }

    @Override
    public void setPresenter(DiscoverySearchAccurateContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return DiscoverySearchAccurateFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    protected void initViews() {
        mIbtnBack = (ImageButton) mRootView.findViewById(R.id.ibtn_back);
        mTvCancel = (TextView) mRootView.findViewById(R.id.tv_cancel);
        mEtSearch = (EditText) mRootView.findViewById(R.id.et_search);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recycler_view);
        mLoadMoreContainer = (LoadMoreRecyclerContainer) mRootView.findViewById(R.id.load_more_container);

        initRecyclerViews();
        initListeners();
    }

    private void initRecyclerViews() {
        mAdapter = new MultiTypeLoadMoreAdapter();
        mAdapter.register(Article.class, new DiscoverySearchArticleProvider(mActivity));

        mLoadMoreContainer.setRecyclerViewAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
    }

    private void initListeners() {
        mIbtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
            }
        });
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.actionStart(mActivity, DiscoveryFragment.TAG);
            }
        });

        mEtSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && !TextUtils.isEmpty(getKeyword())) {
                    mPresenter.search(getKeyword(), true);
                }
                return false;
            }
        });

        mLoadMoreContainer.useDefaultFooter();
        mLoadMoreContainer.setLoadMoreHandler(
                new LoadMoreHandler() {
                    @Override
                    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                        // 加载更多
                        mPresenter.search(getKeyword(), false);
                    }
                });
    }

    @Override
    public String getKeyword() {
        return mEtSearch.getText().toString().trim();
    }

    @Override
    public void setItems(List<?> items) {
        mAdapter.setItems(items);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void setTitleHint(String title) {
        mEtSearch.setHint(title);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_discovery_search_accurate;
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }

    @Override
    public void loadMoreFinish(boolean emptyResult, boolean hasMore) {
        mLoadMoreContainer.loadMoreFinish(emptyResult, hasMore);
    }

    @Override
    public void loadMoreError() {
        mLoadMoreContainer.loadMoreError(0, UIUtils.getString(R.string.load_more_failed));
    }
}
